﻿using mediaControl.Event;
using mediaControl.Utils;
using mediaControl.Vo;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Prism.Commands;
using Prism.Events;
using Prism.Ioc;
using Prism.Mvvm;
using Prism.Services.Dialogs;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Forms;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using MessageBox = System.Windows.Forms.MessageBox;

namespace mediaControl.ViewModels
{
    public class ProgramSettingViewModel : BindableBase
    {
        //事件聚合器
        private readonly IEventAggregator _eventAggregator;

        private readonly IDialogService dialogService;

        //ListBox对象
        private System.Windows.Controls.ListBox listBox;

        private ProgramItemVo selectedItem;

        private ProgramItem selectedTreeItem;

        private ProgramVo program;

        private string fileFullPath;

        private ObservableCollection<ProgramItem> tempList = new ObservableCollection<ProgramItem>();

        private ObservableCollection<ProgramItem> tempSourceList = new ObservableCollection<ProgramItem>();

        private ObservableCollection<ProgramItem> sourceList = new ObservableCollection<ProgramItem>();

        private ObservableCollection<ScreenVo> screenVos = new ObservableCollection<ScreenVo>();

        private ObservableCollection<ScreenItemVo> screenItemVos = new ObservableCollection<ScreenItemVo>();

        public DelegateCommand AddPictureCommand { get; set; }

        public DelegateCommand AddVideoCommand { get; set; }

        public DelegateCommand AddFolderCommand { get; set; }

        public DelegateCommand AddWebpageCommand { get; set; }

        public DelegateCommand AddDesktopCommand { get; set; }

        public DelegateCommand AddBlackCommand { get; set; }

        public DelegateCommand<object> SelectionChangedCommand { get; set; }

        public DelegateCommand<ProgramItem> DeleteCommand { get; set; }

        public DelegateCommand<object> DropFileCommand { get; set; }
        public DelegateCommand<object> SettingCommand { get; set; }

        public DelegateCommand<ProgramItem> SetSelectedItemCommand { get; set; }

        public ProgramItemVo SelectedItem { get { return this.selectedItem; } set { this.selectedItem = value; RaisePropertyChanged(); } }

        public ProgramItem SelectedTreeItem { get { return selectedTreeItem; } set { this.selectedTreeItem = value; RaisePropertyChanged(); } }

        public ObservableCollection<ScreenVo> ScreenVos { get { return screenVos; } set { screenVos = value; RaisePropertyChanged(); } }

        public ObservableCollection<ProgramItem> SourceList { get { return sourceList; } set { this.sourceList = value; RaisePropertyChanged(); } }

        public ProgramSettingViewModel(IEventAggregator eventAggregator, IDialogService dialogService)
        {
            _eventAggregator = eventAggregator;
            this.dialogService = dialogService;
            _eventAggregator.GetEvent<SelectedProgramItemEvent>().Subscribe(Received);
            _eventAggregator.GetEvent<RefreshSorceListEvent>().Subscribe(Received);
            _eventAggregator.GetEvent<SaveSourceListEvent>().Subscribe(SaveSourcelist);
            _eventAggregator.GetEvent<ProgramEvent>().Subscribe(ProgramReceived);
            _eventAggregator.GetEvent<PlaySettingFlagEvent>().Subscribe(PaySettingFlag);
            AddPictureCommand = new DelegateCommand(addPicture);
            AddVideoCommand = new DelegateCommand(addVideo);
            AddFolderCommand = new DelegateCommand(addFolder);
            AddWebpageCommand = new DelegateCommand(addWebpage);
            AddDesktopCommand = new DelegateCommand(AddDeskTop);
            AddBlackCommand = new DelegateCommand(AddBlack);
            DeleteCommand = new DelegateCommand<ProgramItem>(delete);
            SelectionChangedCommand = new DelegateCommand<object>(SelectionChanged);
            SetSelectedItemCommand = new DelegateCommand<ProgramItem>(setSelectedItem);
            SettingCommand = new DelegateCommand<object>(saveSetting);
            DropFileCommand = new DelegateCommand<object>(DropFile);
            initData();

        }

        private void DropFile(object obj)
        {
            var files = (string[])((System.Windows.DragEventArgs)obj).Data.GetData("FileDrop");
            if (files != null && files.Length > 0)
            {
                for (int i = 0; i < files.Length; i++)
                {
                    Thread.Sleep(2);
                    string extension = Path.GetExtension(files[i]).ToLowerInvariant();

                    if (extension == ".jpg" || extension == ".jpeg" || extension == ".png" || extension == ".gif" || extension == ".bmp")
                    {
                        Thread.Sleep(2);
                        long timeStamp = DateTimeOffset.Now.ToUnixTimeMilliseconds();
                        ProgramItem programItem = new ProgramItem("2", files[i], timeStamp + "");
                        ProgramItem programItem1 = new ProgramItem("2", files[i], timeStamp + "");
                        tempSourceList.Add(programItem);
                        SourceList.Add(handleItem(programItem1));
                    }
                    else if (extension == ".mp4" || extension == ".mov" || extension == ".wmv" || extension == ".avi" || extension == ".mkv")
                    {
                        Thread.Sleep(2);
                        long timeStamp = DateTimeOffset.Now.ToUnixTimeMilliseconds();
                        ProgramItem programItem = new ProgramItem("1", files[i], timeStamp + "");
                        ProgramItem programItem1 = new ProgramItem("1", files[i], timeStamp + "");
                        tempSourceList.Add(programItem);
                        SourceList.Add(handleItem(programItem1));
                    }
                }
                string content = WriteFileUtil.readFile(fileFullPath);
                foreach (ProgramItem programItem2 in tempSourceList)
                {
                    programItem2.ImageSource = null;
                }
                if (!String.IsNullOrEmpty(content))
                {
                    JObject programJson = JsonConvert.DeserializeObject<JObject>(content);
                    if (programJson.ContainsKey("sourceList"))
                    {
                        programJson["sourceList"] = JsonConvert.SerializeObject(tempSourceList);
                    }
                    else
                    {
                        programJson.Add("sourceList", JsonConvert.SerializeObject(tempSourceList));
                    }

                    WriteFileUtil.writeFile(fileFullPath, JsonConvert.SerializeObject(programJson), "2");
                }

                // 将MediaElement添加到界面上的某个位置
                //data.Children.Add(mediaElement); // 或者其他布局控件的Children集合中
            }
        }

        private bool playFlag = false;
        private void PaySettingFlag(bool obj)
        {
            PlayFlag = obj;
        }

        private void SaveSourcelist(SaveSourceListVo obj)
        {
            if (!File.Exists(fileFullPath))
            {
                MessageBox.Show("文件不存在！");
                return;
            }

            string content = WriteFileUtil.readFile(fileFullPath);
            if (!String.IsNullOrEmpty(content))
            {
                JObject programJson = JsonConvert.DeserializeObject<JObject>(content);
                if (programJson.ContainsKey("programItems"))
                {
                    programJson["programItems"] = JsonConvert.SerializeObject(obj.ProgramItems);
                }
                else
                {
                    programJson.Add("programItems", JsonConvert.SerializeObject(obj.ProgramItems));
                }
                ObservableCollection<ProgramItem> temps = new ObservableCollection<ProgramItem>();
                foreach (ProgramItem programItem in SourceList) {
                    programItem.ImageSource = null;
                    temps.Add(programItem);
                }
                if (programJson.ContainsKey("sourceList"))
                {
                    programJson["sourceList"] = JsonConvert.SerializeObject(temps);
                }
                else
                {
                    programJson.Add("sourceList", JsonConvert.SerializeObject(temps));
                }
                WriteFileUtil.writeFile(fileFullPath, JsonConvert.SerializeObject(programJson), "2");
            }
        }

        ObservableCollection<ProgramItem> realItems = new ObservableCollection<ProgramItem>();
        private void SelectionChanged(object obj)
        {
            ListBox = obj as System.Windows.Controls.ListBox;
            realItems = (ObservableCollection<ProgramItem>)ListBox.ItemsSource;
        }

        private void AddBlack()
        {
            long timeStamp = DateTimeOffset.Now.ToUnixTimeMilliseconds();
            ProgramItem programItem = new ProgramItem("7", null, timeStamp + "");
            tempSourceList.Add(programItem);
            string content = WriteFileUtil.readFile(fileFullPath);
            foreach (ProgramItem programItem2 in tempSourceList)
            {
                programItem2.ImageSource = null;
            }
            if (!String.IsNullOrEmpty(content))
            {
                JObject programJson = JsonConvert.DeserializeObject<JObject>(content);
                if (programJson.ContainsKey("sourceList"))
                {
                    programJson["sourceList"] = JsonConvert.SerializeObject(tempSourceList);
                }
                else
                {
                    programJson.Add("sourceList", JsonConvert.SerializeObject(tempSourceList));
                }

                WriteFileUtil.writeFile(fileFullPath, JsonConvert.SerializeObject(programJson), "2");
            }
            //通知列表显示
            programItem.DisplayModeTabe = DisplayModeTabe;
            programItem.ExitMethodTabe = ExitMethodTabe;
            programItem.DisplayMode = string.IsNullOrEmpty(programItem.DisplayMode) ? "2" : programItem.DisplayMode;
            programItem.ExitMethod = string.IsNullOrEmpty(programItem.ExitMethod) ? "0" : programItem.ExitMethod;
            //处理屏幕
            ObservableCollection<ScreenItemVo> newScreenItemVos = new ObservableCollection<ScreenItemVo>();
            foreach (ScreenVo screen in ScreenVos)
            {
                bool isSelected = false;
                ScreenItemVo screenItemVo = new ScreenItemVo(screen.Id, screen.ScreenName, isSelected);
                newScreenItemVos.Add(screenItemVo);
            }
            programItem.ScreenItemVos = newScreenItemVos;
            switch (programItem.ItemType)
            {
                case "1":
                    programItem.ImageFlag = Visibility.Visible;
                    programItem.TextFlag = Visibility.Hidden;
                    programItem.MuteVisible = Visibility.Visible;
                    programItem.ResidenceFlag = Visibility.Hidden;
                    programItem.ExitFlag = Visibility.Hidden;
                    programItem.ImageSource = WriteFileUtil.GetThumbnailByPath(programItem.ItemPath);
                    break;
                case "2":
                    programItem.ImageFlag = Visibility.Visible;
                    programItem.TextFlag = Visibility.Hidden;
                    programItem.MuteVisible = Visibility.Hidden;
                    programItem.MuteVisible = Visibility.Hidden;
                    programItem.ResidenceFlag = Visibility.Hidden;
                    programItem.ExitFlag = Visibility.Visible;
                    programItem.ImageSource = WriteFileUtil.GetThumbnailByPath(programItem.ItemPath); ;
                    break;
                case "3":
                    programItem.ImageFlag = Visibility.Visible;
                    programItem.TextFlag = Visibility.Hidden;
                    programItem.MuteVisible = Visibility.Hidden;
                    programItem.ResidenceFlag = Visibility.Visible;
                    programItem.ExitFlag = Visibility.Visible;
                    programItem.ImageSource = WriteFileUtil.GetFolderIcon();
                    break;
                case "4":
                    /*
                    programItem.WebUrl = programItem.ItemPath;
                    programItem.ImageFlag = Visibility.Hidden;
                    programItem.TextFlag = Visibility.Hidden;
                    programItem.WebFlag = Visibility.Visible;*/
                    programItem.ImageFlag = Visibility.Hidden;
                    programItem.TextFlag = Visibility.Visible;
                    programItem.MuteVisible = Visibility.Hidden;
                    programItem.ResidenceFlag = Visibility.Hidden;
                    programItem.ExitFlag = Visibility.Hidden;
                    programItem.ItemTypeStr = "网页框";
                    break;
                case "5":
                    programItem.ImageFlag = Visibility.Hidden;
                    programItem.TextFlag = Visibility.Visible;
                    programItem.MuteVisible = Visibility.Hidden;
                    programItem.ResidenceFlag = Visibility.Hidden;
                    programItem.ExitFlag = Visibility.Hidden;
                    programItem.ItemTypeStr = "采集卡";
                    break;
                case "6":
                    programItem.ImageFlag = Visibility.Hidden;
                    programItem.TextFlag = Visibility.Visible;
                    programItem.MuteVisible = Visibility.Hidden;
                    programItem.ResidenceFlag = Visibility.Hidden;
                    programItem.ExitFlag = Visibility.Visible;
                    programItem.ItemTypeStr = "电脑桌面";
                    break;
                case "7":
                    programItem.ImageFlag = Visibility.Hidden;
                    programItem.TextFlag = Visibility.Visible;
                    programItem.MuteVisible = Visibility.Hidden;
                    programItem.ResidenceFlag = Visibility.Hidden;
                    programItem.ExitFlag = Visibility.Hidden;
                    programItem.ItemTypeStr = "黑场";
                    break;
                default:
                    //programItem.ItemType = "文件";
                    programItem.MuteVisible = Visibility.Hidden;
                    programItem.ResidenceFlag = Visibility.Hidden;
                    programItem.ExitFlag = Visibility.Hidden;
                    break;
            }
            SourceList.Add(programItem);
        }

        private void AddDeskTop()
        {
            long timeStamp = DateTimeOffset.Now.ToUnixTimeMilliseconds();
            ProgramItem programItem = new ProgramItem("6", null, timeStamp + "");
            //sourceList.Add(programItem);
            tempSourceList.Add(programItem);
            string content = WriteFileUtil.readFile(fileFullPath);
            foreach (ProgramItem programItem2 in tempSourceList)
            {
                programItem2.ImageSource = null;
            }
            if (!String.IsNullOrEmpty(content))
            {
                JObject programJson = JsonConvert.DeserializeObject<JObject>(content);
                if (programJson.ContainsKey("sourceList"))
                {
                    programJson["sourceList"] = JsonConvert.SerializeObject(tempSourceList);
                }
                else
                {
                    programJson.Add("sourceList", JsonConvert.SerializeObject(tempSourceList));
                }

                WriteFileUtil.writeFile(fileFullPath, JsonConvert.SerializeObject(programJson), "2");
            }
            //通知列表显示
            programItem.DisplayModeTabe = DisplayModeTabe;
            programItem.ExitMethodTabe = ExitMethodTabe;
            programItem.DisplayMode = string.IsNullOrEmpty(programItem.DisplayMode) ? "2" : programItem.DisplayMode;
            programItem.ExitMethod = string.IsNullOrEmpty(programItem.ExitMethod) ? "0" : programItem.ExitMethod;
            //处理屏幕
            ObservableCollection<ScreenItemVo> newScreenItemVos = new ObservableCollection<ScreenItemVo>();
            foreach (ScreenVo screen in ScreenVos)
            {
                bool isSelected = false;
                ScreenItemVo screenItemVo = new ScreenItemVo(screen.Id, screen.ScreenName, isSelected);
                newScreenItemVos.Add(screenItemVo);
            }
            programItem.ScreenItemVos = newScreenItemVos;
            switch (programItem.ItemType)
            {
                case "1":
                    programItem.ImageFlag = Visibility.Visible;
                    programItem.TextFlag = Visibility.Hidden;
                    programItem.MuteVisible = Visibility.Visible;
                    programItem.ResidenceFlag = Visibility.Hidden;
                    programItem.ExitFlag = Visibility.Hidden;
                    programItem.ImageSource = WriteFileUtil.GetThumbnailByPath(programItem.ItemPath);
                    break;
                case "2":
                    programItem.ImageFlag = Visibility.Visible;
                    programItem.TextFlag = Visibility.Hidden;
                    programItem.MuteVisible = Visibility.Hidden;
                    programItem.ResidenceFlag = Visibility.Hidden;
                    programItem.ExitFlag = Visibility.Visible;
                    programItem.ImageSource = WriteFileUtil.GetThumbnailByPath(programItem.ItemPath); ;
                    break;
                case "3":
                    programItem.ImageFlag = Visibility.Visible;
                    programItem.TextFlag = Visibility.Hidden;
                    programItem.MuteVisible = Visibility.Hidden;
                    programItem.ResidenceFlag = Visibility.Visible;
                    programItem.ExitFlag = Visibility.Visible;
                    programItem.ImageSource = WriteFileUtil.GetFolderIcon();
                    break;
                case "4":
                    /*
                    programItem.WebUrl = programItem.ItemPath;
                    programItem.ImageFlag = Visibility.Hidden;
                    programItem.TextFlag = Visibility.Hidden;
                    programItem.WebFlag = Visibility.Visible;*/
                    programItem.ImageFlag = Visibility.Hidden;
                    programItem.TextFlag = Visibility.Visible;
                    programItem.MuteVisible = Visibility.Hidden;
                    programItem.ResidenceFlag = Visibility.Hidden;
                    programItem.ExitFlag = Visibility.Hidden;
                    programItem.ItemTypeStr = "网页框";
                    break;
                case "5":
                    programItem.ImageFlag = Visibility.Hidden;
                    programItem.TextFlag = Visibility.Visible;
                    programItem.MuteVisible = Visibility.Hidden;
                    programItem.ResidenceFlag = Visibility.Hidden;
                    programItem.ExitFlag = Visibility.Hidden;
                    programItem.ItemTypeStr = "采集卡";
                    break;
                case "6":
                    programItem.ImageFlag = Visibility.Hidden;
                    programItem.TextFlag = Visibility.Visible;
                    programItem.MuteVisible = Visibility.Hidden;
                    programItem.ResidenceFlag = Visibility.Hidden;
                    programItem.ExitFlag = Visibility.Visible;
                    programItem.ItemTypeStr = "电脑桌面";
                    break;
                case "7":
                    programItem.ImageFlag = Visibility.Hidden;
                    programItem.TextFlag = Visibility.Visible;
                    programItem.MuteVisible = Visibility.Hidden;
                    programItem.ResidenceFlag = Visibility.Hidden;
                    programItem.ExitFlag = Visibility.Hidden;
                    programItem.ItemTypeStr = "黑场";
                    break;
                default:
                    //programItem.ItemType = "文件";
                    programItem.MuteVisible = Visibility.Hidden;
                    programItem.ResidenceFlag = Visibility.Hidden;
                    programItem.ExitFlag = Visibility.Hidden;
                    break;
            }
            SourceList.Add(programItem);
        }

        public ProgramSettingViewModel()
        {
        }

        private void ProgramReceived(ProgramVo obj)
        {
            Program = obj;
            fileFullPath = obj.FilePath + "\\" + obj.Name + ".info";
            //获取项目的素材列表
            if (!File.Exists(fileFullPath))
            {
                MessageBox.Show("文件不存在！");
                return;
            }
            string content = WriteFileUtil.readFile(fileFullPath);
            if (!String.IsNullOrEmpty(content))
            {
                JObject programJson = JsonConvert.DeserializeObject<JObject>(content);
                if (programJson.ContainsKey("sourceList"))
                {
                    tempSourceList = JsonConvert.DeserializeObject<ObservableCollection<ProgramItem>>((String)programJson.GetValue("sourceList"));
                }
                else
                {
                    tempSourceList = new ObservableCollection<ProgramItem>();
                }
            }
            initData();
        }

        private void saveSetting(object obj)
        {
            if (playFlag) {
                MessageBox.Show("有节目播放中，不能修改！");
                return;
            }
            if (null == SelectedTreeItem || null == SelectedItem)
            {
                return;
            }
            SelectedTreeItem.IsLink = false;
            //判断当前是否关联屏幕
            foreach(ScreenItemVo screenItem in SelectedTreeItem.ScreenItemVos)
            {
                if (screenItem.IsSelected) { 
                    SelectedTreeItem.IsLink = true;
                    break;
                }
            }
            selectedTreeItem.ImageSource = null;
            ObservableCollection<ProgramItem> linkedList = SelectedItem.SourceList;
            bool flag = false;
            //保存节目关联配置
            if (null != linkedList && linkedList.Count>0)
            {
                //先移除删除了的素材
                ObservableCollection<ProgramItem> sourceList = tempSourceList;
                List<string> sourceIds = new List<string>();
                foreach (ProgramItem programItem2 in sourceList)
                {
                    sourceIds.Add(programItem2.No);
                }
                ObservableCollection<ProgramItem> tempList = new ObservableCollection<ProgramItem>();
                foreach (ProgramItem programItem1 in linkedList)
                {
                    if (sourceIds.Contains(programItem1.No))
                    {
                        tempList.Add(programItem1);
                    }
                }
                linkedList.Clear();
                if (null != tempList && tempList.Count > 0)
                {
                    foreach (ProgramItem programItem2 in tempList)
                    {
                        programItem2.ImageSource = null;
                        linkedList.Add(programItem2);
                    }
                }
                //控制同一个屏幕只能选同一类型的素材
                if (null != linkedList && linkedList.Count > 0 && SelectedTreeItem.IsLink)
                {
                    List<ScreenTypeVo> screenTypes = new List<ScreenTypeVo>();
                    foreach (ProgramItem item in linkedList) {
                        ObservableCollection<ScreenItemVo> itemVos = item.ScreenItemVos;
                        if (null != itemVos && itemVos.Count > 0) {
                            foreach (ScreenItemVo screenItemVo in itemVos) {
                                if (screenItemVo.IsSelected)
                                {
                                    ScreenTypeVo screenTypeVo = new ScreenTypeVo();
                                    screenTypeVo.ItemType = item.ItemType;
                                    screenTypeVo.Id = screenItemVo.Id;
                                    screenTypes.Add(screenTypeVo);
                                }
                            }
                        }
                    }
                    List<ScreenTypeVo> compareList = new List<ScreenTypeVo>();
                    foreach (ScreenItemVo screenItemVo1 in SelectedTreeItem.ScreenItemVos) {
                        if (screenItemVo1.IsSelected)
                        {
                            ScreenTypeVo screenTypeVo1 = new ScreenTypeVo();
                            screenTypeVo1.ItemType = selectedTreeItem.ItemType;
                            screenTypeVo1.Id = screenItemVo1.Id;
                            compareList.Add(screenTypeVo1);
                        }
                    }
                    if (screenTypes.Count > 0 && compareList.Count>0) {
                        foreach (ScreenTypeVo screenTypeVo2 in compareList) {
                            foreach (ScreenTypeVo screenTypeVo3 in screenTypes) {
                                if (screenTypeVo2.Id == screenTypeVo3.Id && !screenTypeVo2.ItemType.Equals(screenTypeVo3.ItemType)) {
                                    foreach (ScreenItemVo screenItemVo2 in SelectedTreeItem.ScreenItemVos)
                                    {
                                        if (screenItemVo2.Id == screenTypeVo2.Id)
                                        {
                                            screenItemVo2.IsSelected = false;
                                            break;
                                        }
                                    }
                                    ObservableCollection<ProgramItem> tempProgramItems = new ObservableCollection<ProgramItem>();
                                    foreach (ProgramItem programItem in SourceList) {
                                        if (programItem.No.Equals(SelectedTreeItem.No)) {
                                            programItem.ScreenItemVos = SelectedTreeItem.ScreenItemVos;
                                        }
                                        if (null == programItem.ImageSource) {
                                            //programItem.ImageSource = GetIcon(programItem.ItemType);

                                        }
                                        programItem.DisplayMode = string.IsNullOrEmpty(programItem.DisplayMode) ? "2" : programItem.DisplayMode;
                                        programItem.ExitMethod = string.IsNullOrEmpty(programItem.ExitMethod) ? "0" : programItem.ExitMethod;
                                        switch (programItem.ItemType)
                                        {
                                            case "1":
                                                programItem.ImageFlag = Visibility.Visible;
                                                programItem.TextFlag = Visibility.Hidden;
                                                programItem.MuteVisible = Visibility.Visible;
                                                programItem.ResidenceFlag = Visibility.Hidden;
                                                programItem.ExitFlag = Visibility.Hidden;
                                                programItem.ImageSource = WriteFileUtil.GetThumbnailByPath(programItem.ItemPath);
                                                break;
                                            case "2":
                                                programItem.ImageFlag = Visibility.Visible;
                                                programItem.TextFlag = Visibility.Hidden;
                                                programItem.MuteVisible = Visibility.Hidden;
                                                programItem.ResidenceFlag = Visibility.Hidden;
                                                programItem.ExitFlag = Visibility.Visible;
                                                programItem.ImageSource = WriteFileUtil.GetThumbnailByPath(programItem.ItemPath); ;
                                                break;
                                            case "3":
                                                programItem.ImageFlag = Visibility.Visible;
                                                programItem.TextFlag = Visibility.Hidden;
                                                programItem.MuteVisible = Visibility.Hidden;
                                                programItem.ResidenceFlag = Visibility.Visible;
                                                programItem.ExitFlag = Visibility.Visible;
                                                programItem.ImageSource = WriteFileUtil.GetFolderIcon();
                                                break;
                                            case "4":
                                                /*
                                                programItem2.WebUrl = programItem2.ItemPath;
                                                programItem2.ImageFlag = Visibility.Hidden;
                                                programItem2.TextFlag = Visibility.Hidden;
                                                programItem2.WebFlag = Visibility.Visible;
                                                */
                                                programItem.ImageFlag = Visibility.Hidden;
                                                programItem.TextFlag = Visibility.Visible;
                                                programItem.MuteVisible = Visibility.Hidden;
                                                programItem.ResidenceFlag = Visibility.Hidden;
                                                programItem.ExitFlag = Visibility.Hidden;
                                                programItem.ItemTypeStr = "网页框";
                                                break;
                                            case "5":
                                                programItem.ImageFlag = Visibility.Hidden;
                                                programItem.TextFlag = Visibility.Visible;
                                                programItem.MuteVisible = Visibility.Hidden;
                                                programItem.ResidenceFlag = Visibility.Hidden;
                                                programItem.ExitFlag = Visibility.Hidden;
                                                programItem.ItemTypeStr = "采集卡";
                                                break;
                                            case "6":
                                                programItem.ImageFlag = Visibility.Hidden;
                                                programItem.TextFlag = Visibility.Visible;
                                                programItem.MuteVisible = Visibility.Hidden;
                                                programItem.ExitFlag = Visibility.Visible;
                                                programItem.ItemTypeStr = "电脑桌面";
                                                break;
                                            case "7":
                                                programItem.ImageFlag = Visibility.Hidden;
                                                programItem.TextFlag = Visibility.Visible;
                                                programItem.MuteVisible = Visibility.Hidden;
                                                programItem.ResidenceFlag = Visibility.Hidden;
                                                programItem.ItemTypeStr = "黑场";
                                                break;
                                            default:
                                                //programItem.ItemType = "文件";
                                                programItem.MuteVisible = Visibility.Hidden;
                                                programItem.ResidenceFlag = Visibility.Hidden;
                                                programItem.ExitFlag = Visibility.Hidden;
                                                break;
                                        }
                                        tempProgramItems.Add(programItem);
                                    }
                                    SourceList.Clear();

                                    foreach (var programItem2 in tempProgramItems) {
                                        SourceList.Add(programItem2);
                                    }
                                    MessageBox.Show("同一个屏幕只能选择同一类型的素材");

                                    return;
                                }
                            }
                        }
                    }
                }
                if (null != linkedList && linkedList.Count > 0)
                {
                    foreach (ProgramItem programItem in linkedList)
                    {
                        if (programItem.No.Equals(SelectedTreeItem.No))
                        {
                            if (SelectedTreeItem.IsLink)
                            {
                                programItem.IsMute = SelectedTreeItem.IsMute;
                                programItem.PlayMode = SelectedTreeItem.PlayMode;
                                programItem.ResidenceTime = SelectedTreeItem.ResidenceTime;
                                programItem.ScreenItemVos = SelectedTreeItem.ScreenItemVos;
                                programItem.DisplayMode = SelectedTreeItem.DisplayMode;
                                programItem.ExitMethod = SelectedTreeItem.ExitMethod;
                            }
                            else
                            {
                                linkedList.Remove(programItem);
                            }
                            flag = true;
                            break;
                        }
                    }
                }

            }
            else
            {
                linkedList = new ObservableCollection<ProgramItem>();
            }
            if (!flag)
            {

                if (SelectedTreeItem.IsLink)
                {
                    linkedList.Add(selectedTreeItem);
                }
            }
            //写入文件
            try
            {

                string content = WriteFileUtil.readFile(fileFullPath);
                if (!String.IsNullOrEmpty(content))
                {
                    JObject programJson = JsonConvert.DeserializeObject<JObject>(content);
                    ObservableCollection<ProgramItemVo> programItemVos = new ObservableCollection<ProgramItemVo>();
                    if (programJson.ContainsKey("programItems"))
                    {
                        programItemVos = JsonConvert.DeserializeObject<ObservableCollection<ProgramItemVo>>((String)programJson.GetValue("programItems"));
                        foreach (ProgramItemVo itemVo in programItemVos)
                        {
                            if (itemVo.Id == SelectedItem.Id)
                            {
                                itemVo.SourceList = linkedList;
                                SelectedItem.SourceList = linkedList;
                                break;
                            }
                        }

                        programJson["programItems"] = JsonConvert.SerializeObject(programItemVos);
                        WriteFileUtil.writeFile(fileFullPath, JsonConvert.SerializeObject(programJson), "2");
                        _eventAggregator.GetEvent<ProgramItemEvent>().Publish(SelectedItem);
                        _eventAggregator.GetEvent<RefreshProgramListEvent>().Publish(JsonConvert.SerializeObject(programItemVos));
                    }
                }

            }
            catch (Exception ex)
            {

            }
        }

        private void setSelectedItem(ProgramItem obj)
        {
            
            if (null != obj)
            {
                SelectedTreeItem = obj;
            }
        }

        private void delete(ProgramItem obj)
        {
            if (null == selectedTreeItem)
            {
                MessageBox.Show("请先选择素材！");
            }

            //删除list中的
            foreach (ProgramItem programItem3 in tempSourceList)
            {
                if (programItem3.No.Equals(SelectedTreeItem.No))
                {
                    tempSourceList.Remove(programItem3);
                    break;
                }
            }
            ObservableCollection<ProgramItem> linkedList = null;
            if (null != selectedItem)
            {
                linkedList = SelectedItem.SourceList;
            }
            //移除关联的素材
            if (null != linkedList && linkedList.Count > 0) {
                foreach (ProgramItem programItem in linkedList) {
                    if (programItem.No.Equals(SelectedTreeItem.No)) {
                        linkedList.Remove(programItem);
                        break;
                    }
                }
                SelectedItem.SourceList = linkedList;
            }
            SourceList.Remove(selectedTreeItem);
            /*
            ObservableCollection<ProgramItem> tempProgramItems = new ObservableCollection<ProgramItem>();
            foreach (ProgramItem programItem2 in SourceList) {
                tempProgramItems.Add(programItem2);
            }
            SourceList.Clear();
            foreach (ProgramItem programItem3 in tempProgramItems) {
                SourceList.Add(programItem3);
            }*/
            ObservableCollection<ProgramItem> saveList = new ObservableCollection<ProgramItem>();
            foreach (ProgramItem programItem3 in tempSourceList)
            {
                ProgramItem programItem4 = new ProgramItem();
                programItem4.ItemPath = programItem3.ItemPath;
                programItem4.ItemType = programItem3.ItemType;
                programItem4.No = programItem3.No;
                programItem4.ImageSource = null;
                programItem4.IsLink = false;
                saveList.Add(programItem4);
            }
            string content = WriteFileUtil.readFile(fileFullPath);
            if (!String.IsNullOrEmpty(content))
            {
                JObject programJson = JsonConvert.DeserializeObject<JObject>(content);
                if (programJson.ContainsKey("sourceList"))
                {
                    programJson["sourceList"] = JsonConvert.SerializeObject(saveList);
                }
                else
                {
                    programJson.Add("sourceList", JsonConvert.SerializeObject(saveList));
                }
                WriteFileUtil.writeFile(fileFullPath, JsonConvert.SerializeObject(programJson), "2");
                _eventAggregator.GetEvent<ProgramItemEvent>().Publish(SelectedItem);
            }

        }

        private void addWebpage()
        {

            IDialogParameters parameters = new DialogParameters();
            dialogService.ShowDialog("Webpage", parameters, callback);
        }

        private void callback(IDialogResult result)
        {
            if (result.Result == ButtonResult.OK)
            {
                string itemPath = result.Parameters.GetValue<string>("itemPath");
                if (null != itemPath)
                {
                    /*string content = "";//返回的结果字符串
                    if (File.Exists(sourceFilePath))
                    {
                        using (StreamReader sr = new StreamReader(sourceFilePath))
                        {
                            content = sr.ReadToEnd();//一次性把文本内容读完
                        }
                    }
                    List<ProgramItem> sourceList = new List<ProgramItem>();
                    if (!String.IsNullOrEmpty(content))
                    {
                        sourceList = JsonConvert.DeserializeObject<List<ProgramItem>>(content);
                    }*/
                    long timeStamp = DateTimeOffset.Now.ToUnixTimeMilliseconds();
                    ProgramItem programItem = new ProgramItem("4", itemPath, timeStamp + "");
                    //sourceList.Add(programItem);
                    tempSourceList.Add(programItem);
                    foreach (ProgramItem programItem2 in tempSourceList)
                    {
                        programItem2.ImageSource = null;
                    }
                    string content = WriteFileUtil.readFile(fileFullPath);
                    if (!String.IsNullOrEmpty(content))
                    {
                        JObject programJson = JsonConvert.DeserializeObject<JObject>(content);
                        if (programJson.ContainsKey("sourceList"))
                        {
                            programJson["sourceList"] = JsonConvert.SerializeObject(tempSourceList);
                        }
                        else
                        {
                            programJson.Add("sourceList", JsonConvert.SerializeObject(tempSourceList));
                        }

                        WriteFileUtil.writeFile(fileFullPath, JsonConvert.SerializeObject(programJson), "2");
                    }
                    //通知列表显示
                    programItem.DisplayModeTabe = DisplayModeTabe;
                    programItem.ExitMethodTabe = ExitMethodTabe;
                    programItem.DisplayMode = string.IsNullOrEmpty(programItem.DisplayMode) ? "2" : programItem.DisplayMode;
                    programItem.ExitMethod = string.IsNullOrEmpty(programItem.ExitMethod) ? "0" : programItem.ExitMethod;
                    //处理屏幕
                    ObservableCollection<ScreenItemVo> newScreenItemVos = new ObservableCollection<ScreenItemVo>();
                    foreach (ScreenVo screen in ScreenVos)
                    {
                        bool isSelected = false;
                        ScreenItemVo screenItemVo = new ScreenItemVo(screen.Id, screen.ScreenName, isSelected);
                        newScreenItemVos.Add(screenItemVo);
                    }
                    programItem.ScreenItemVos = newScreenItemVos;
                    switch (programItem.ItemType)
                    {
                        case "1":
                            programItem.ImageFlag = Visibility.Visible;
                            programItem.TextFlag = Visibility.Hidden;
                            programItem.MuteVisible = Visibility.Visible;
                            programItem.ResidenceFlag = Visibility.Hidden;
                            programItem.ExitFlag = Visibility.Hidden;
                            programItem.ImageSource = WriteFileUtil.GetThumbnailByPath(programItem.ItemPath);
                            break;
                        case "2":
                            programItem.ImageFlag = Visibility.Visible;
                            programItem.TextFlag = Visibility.Hidden;
                            programItem.MuteVisible = Visibility.Hidden;
                            programItem.ResidenceFlag = Visibility.Hidden;
                            programItem.ResidenceFlag = Visibility.Hidden;
                            programItem.ExitFlag = Visibility.Visible;
                            programItem.ImageSource = WriteFileUtil.GetThumbnailByPath(programItem.ItemPath); ;
                            break;
                        case "3":
                            programItem.ImageFlag = Visibility.Visible;
                            programItem.TextFlag = Visibility.Hidden;
                            programItem.MuteVisible = Visibility.Hidden;
                            programItem.ResidenceFlag = Visibility.Visible;
                            programItem.ExitFlag = Visibility.Visible;
                            programItem.ImageSource = WriteFileUtil.GetFolderIcon();
                            break;
                        case "4":
                            /*
                            programItem.WebUrl = programItem.ItemPath;
                            programItem.ImageFlag = Visibility.Hidden;
                            programItem.TextFlag = Visibility.Hidden;
                            programItem.WebFlag = Visibility.Visible;*/
                            programItem.ImageFlag = Visibility.Hidden;
                            programItem.TextFlag = Visibility.Visible;
                            programItem.MuteVisible = Visibility.Hidden;
                            programItem.ResidenceFlag = Visibility.Hidden;
                            programItem.ExitFlag = Visibility.Hidden;
                            programItem.ItemTypeStr = "网页框";
                            break;
                        case "5":
                            programItem.ImageFlag = Visibility.Hidden;
                            programItem.TextFlag = Visibility.Visible;
                            programItem.MuteVisible = Visibility.Hidden;
                            programItem.ResidenceFlag = Visibility.Hidden;
                            programItem.ExitFlag = Visibility.Hidden;
                            programItem.ItemTypeStr = "采集卡";
                            break;
                        case "6":
                            programItem.ImageFlag = Visibility.Hidden;
                            programItem.TextFlag = Visibility.Visible;
                            programItem.MuteVisible = Visibility.Hidden;
                            programItem.ResidenceFlag = Visibility.Hidden;
                            programItem.ExitFlag = Visibility.Visible;
                            programItem.ItemTypeStr = "电脑桌面";
                            break;
                        case "7":
                            programItem.ImageFlag = Visibility.Hidden;
                            programItem.TextFlag = Visibility.Visible;
                            programItem.MuteVisible = Visibility.Hidden;
                            programItem.ResidenceFlag = Visibility.Hidden;
                            programItem.ExitFlag = Visibility.Hidden;
                            programItem.ItemTypeStr = "黑场";
                            break;
                        default:
                            //programItem.ItemType = "文件";
                            programItem.MuteVisible = Visibility.Hidden;
                            programItem.ResidenceFlag = Visibility.Hidden;
                            programItem.ExitFlag = Visibility.Hidden;
                            break;
                    }
                    SourceList.Add(programItem);
                }
            }
        }

        private void addFolder()
        {

            FolderBrowserDialog folderBrowserDialog = new FolderBrowserDialog();
            folderBrowserDialog.ShowDialog();        //这个方法可以显示文件夹选择对话框
            string folderPath = folderBrowserDialog.SelectedPath;    //获取选择的文件夹的全路径名
            if (String.IsNullOrEmpty(folderPath))
            {
                MessageBox.Show("请选择文件夹！");
                return;
            }
            long timeStamp = DateTimeOffset.Now.ToUnixTimeMilliseconds();
            ProgramItem programItem = new ProgramItem("3", folderPath, timeStamp + "");
            tempSourceList.Add(programItem);
            foreach (ProgramItem programItem2 in  tempSourceList) {
                programItem2.ImageSource = null;
            }
            string content = WriteFileUtil.readFile(fileFullPath);
            if (!String.IsNullOrEmpty(content))
            {
                JObject programJson = JsonConvert.DeserializeObject<JObject>(content);
                if (programJson.ContainsKey("sourceList"))
                {
                    programJson["sourceList"] = JsonConvert.SerializeObject(tempSourceList);
                }
                else
                {
                    programJson.Add("sourceList", JsonConvert.SerializeObject(tempSourceList));
                }

                WriteFileUtil.writeFile(fileFullPath, JsonConvert.SerializeObject(programJson), "2");
            }
            //通知列表显示
            programItem.DisplayModeTabe = DisplayModeTabe;
            programItem.ExitMethodTabe = ExitMethodTabe;
            programItem.DisplayMode = string.IsNullOrEmpty(programItem.DisplayMode) ? "2" : programItem.DisplayMode;
            programItem.ExitMethod = string.IsNullOrEmpty(programItem.ExitMethod) ? "0" : programItem.ExitMethod;
            //处理屏幕
            ObservableCollection<ScreenItemVo> newScreenItemVos = new ObservableCollection<ScreenItemVo>();
            foreach (ScreenVo screen in ScreenVos)
            {
                bool isSelected = false;
                ScreenItemVo screenItemVo = new ScreenItemVo(screen.Id, screen.ScreenName, isSelected);
                newScreenItemVos.Add(screenItemVo);
            }
            programItem.ScreenItemVos = newScreenItemVos;
            switch (programItem.ItemType)
            {
                case "1":
                    programItem.ImageFlag = Visibility.Visible;
                    programItem.TextFlag = Visibility.Hidden;
                    programItem.MuteVisible = Visibility.Visible; 
                    programItem.ResidenceFlag = Visibility.Hidden;
                    programItem.ExitFlag = Visibility.Hidden;
                    programItem.ImageSource = WriteFileUtil.GetThumbnailByPath(programItem.ItemPath);
                    break;
                case "2":
                    programItem.ImageFlag = Visibility.Visible;
                    programItem.TextFlag = Visibility.Hidden;
                    programItem.MuteVisible = Visibility.Hidden;
                    programItem.ResidenceFlag = Visibility.Hidden;
                    programItem.ExitFlag = Visibility.Visible;
                    programItem.ImageSource = WriteFileUtil.GetThumbnailByPath(programItem.ItemPath); ;
                    break;
                case "3":
                    programItem.ImageFlag = Visibility.Visible;
                    programItem.TextFlag = Visibility.Hidden;
                    programItem.MuteVisible = Visibility.Hidden;
                    programItem.ResidenceFlag = Visibility.Visible;
                    programItem.ExitFlag = Visibility.Visible;
                    programItem.ImageSource = WriteFileUtil.GetFolderIcon();
                    break;
                case "4":
                    /*
                    programItem.WebUrl = programItem.ItemPath;
                    programItem.ImageFlag = Visibility.Hidden;
                    programItem.TextFlag = Visibility.Hidden;
                    programItem.WebFlag = Visibility.Visible;*/
                    programItem.ImageFlag = Visibility.Hidden;
                    programItem.TextFlag = Visibility.Visible;
                    programItem.MuteVisible = Visibility.Hidden;
                    programItem.ResidenceFlag = Visibility.Hidden; 
                    programItem.ExitFlag = Visibility.Hidden;
                    programItem.ItemTypeStr = "网页框";
                    break;
                case "5":
                    programItem.ImageFlag = Visibility.Hidden;
                    programItem.TextFlag = Visibility.Visible;
                    programItem.MuteVisible = Visibility.Hidden;
                    programItem.ResidenceFlag = Visibility.Hidden;
                    programItem.ExitFlag = Visibility.Hidden;
                    programItem.ItemTypeStr = "采集卡";
                    break;
                case "6":
                    programItem.ImageFlag = Visibility.Hidden;
                    programItem.TextFlag = Visibility.Visible;
                    programItem.MuteVisible = Visibility.Hidden;
                    programItem.ResidenceFlag = Visibility.Hidden;
                    programItem.ExitFlag = Visibility.Visible;
                    programItem.ItemTypeStr = "电脑桌面";
                    break;
                case "7":
                    programItem.ImageFlag = Visibility.Hidden;
                    programItem.TextFlag = Visibility.Visible;
                    programItem.MuteVisible = Visibility.Hidden;
                    programItem.ResidenceFlag = Visibility.Hidden;
                    programItem.ExitFlag = Visibility.Hidden;
                    programItem.ItemTypeStr = "黑场";
                    break;
                default:
                    //programItem.ItemType = "文件";
                    programItem.MuteVisible = Visibility.Hidden;
                    programItem.ResidenceFlag = Visibility.Hidden;
                    programItem.ExitFlag = Visibility.Hidden;
                    break;
            }
            SourceList.Add(programItem);
        }

        private void addVideo()
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Title = "选择视频";
            openFileDialog.Filter = "视频文件|*.mp4; *.avi; *.wmv; *.mov";
            openFileDialog.Multiselect = true;
            openFileDialog.FilterIndex = 1;
            if (openFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                foreach (string item in openFileDialog.FileNames)
                {

                    if (String.IsNullOrEmpty(item))
                    {
                        MessageBox.Show("请选择视频！");
                        return;
                    }
                    Thread.Sleep(2);
                    long timeStamp = DateTimeOffset.Now.ToUnixTimeMilliseconds();
                    ProgramItem programItem = new ProgramItem("1", item, timeStamp + "");
                    tempSourceList.Add(programItem);
                    ProgramItem programItem1 = new ProgramItem("1", item, timeStamp + "");
                    SourceList.Add(handleItem(programItem1));
                }
                string content = WriteFileUtil.readFile(fileFullPath);
                foreach (ProgramItem programItem2 in tempSourceList)
                {
                    programItem2.ImageSource = null;
                }
                if (!String.IsNullOrEmpty(content))
                {
                    JObject programJson = JsonConvert.DeserializeObject<JObject>(content);
                    if (programJson.ContainsKey("sourceList"))
                    {
                        programJson["sourceList"] = JsonConvert.SerializeObject(tempSourceList);
                    }
                    else
                    {
                        programJson.Add("sourceList", JsonConvert.SerializeObject(tempSourceList));
                    }

                    WriteFileUtil.writeFile(fileFullPath, JsonConvert.SerializeObject(programJson), "2");
                }
            }
        }

        private void addPicture()
        {

            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Title = "选择图片";
            openFileDialog.Filter = "Image Files (*.jpg;*.jpeg;*.png)|*.jpg;*.jpeg;*.png";
            openFileDialog.Multiselect = true;
            openFileDialog.FilterIndex = 1;
            if (openFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                foreach (string item in openFileDialog.FileNames)
                {
                    string filePath = item;

                    if (String.IsNullOrEmpty(filePath))
                    {
                        MessageBox.Show("请选择图片！");
                        return;
                    }
                    Thread.Sleep(2);
                    long timeStamp = DateTimeOffset.Now.ToUnixTimeMilliseconds();
                    ProgramItem programItem = new ProgramItem("2", filePath, timeStamp + "");
                    ProgramItem programItem1 = new ProgramItem("2", filePath, timeStamp + "");
                    tempSourceList.Add(programItem);
                    SourceList.Add(handleItem(programItem1));
                    
                }
                string content = WriteFileUtil.readFile(fileFullPath);
                foreach (ProgramItem programItem2 in tempSourceList)
                {
                    programItem2.ImageSource = null;
                }
                if (!String.IsNullOrEmpty(content))
                {
                    JObject programJson = JsonConvert.DeserializeObject<JObject>(content);
                    if (programJson.ContainsKey("sourceList"))
                    {
                        programJson["sourceList"] = JsonConvert.SerializeObject(tempSourceList);
                    }
                    else
                    {
                        programJson.Add("sourceList", JsonConvert.SerializeObject(tempSourceList));
                    }

                    WriteFileUtil.writeFile(fileFullPath, JsonConvert.SerializeObject(programJson), "2");
                }
               
                
            }

        }



        private void Received(ProgramItemVo obj)
        {
            if (null == obj) {
                return;
            }
            
            initScreenList();
            ObservableCollection<ProgramItem> linkedList = null;
            SelectedItem = obj;
            linkedList = SelectedItem.SourceList;
            ObservableCollection<ProgramItem> tempList1 = new ObservableCollection<ProgramItem>();
            //SourceList.Clear();
            foreach (ProgramItem programItem in SourceList)
            {
                ProgramItem linkItem = new ProgramItem();
                programItem.IsLink = false;
                programItem.ScreenItemVos = null;
                if (null != linkedList && linkedList.Count > 0)
                {
                    foreach (ProgramItem programItem1 in linkedList)
                    {
                        if (programItem1.No.Equals(programItem.No))
                        {
                            linkItem = programItem1;
                            break;
                        }
                    }
                }
                if (null != linkItem)
                {
                    programItem.DisplayMode = linkItem.DisplayMode;
                    //programItem.IsLink = true;
                    programItem.IsMute = linkItem.IsMute;
                    programItem.PlayMode = linkItem.PlayMode;
                    programItem.ExitMethod = linkItem.ExitMethod;
                    programItem.ResidenceTime = linkItem.ResidenceTime;
                }
                //处理类型
                switch (programItem.ItemType)
                {
                    case "1":
                        programItem.ImageFlag = Visibility.Visible;
                        programItem.TextFlag = Visibility.Hidden;
                        programItem.ImageSource = WriteFileUtil.GetThumbnailByPath(programItem.ItemPath);
                        programItem.MuteVisible = Visibility.Visible;
                        programItem.ExitFlag = Visibility.Hidden;
                        programItem.ResidenceFlag = Visibility.Hidden;
                        break;
                    case "2":
                        programItem.ImageFlag = Visibility.Visible;
                        programItem.TextFlag = Visibility.Hidden;
                        programItem.MuteVisible = Visibility.Hidden;
                        programItem.ExitFlag = Visibility.Visible;
                        programItem.ImageSource = WriteFileUtil.GetThumbnailByPath(programItem.ItemPath);
                        programItem.ResidenceFlag = Visibility.Hidden;
                        break;
                    case "3":
                        programItem.ImageFlag = Visibility.Visible;
                        programItem.TextFlag = Visibility.Hidden;
                        programItem.MuteVisible = Visibility.Hidden;
                        programItem.ResidenceFlag = Visibility.Visible;
                        programItem.ExitFlag = Visibility.Visible;
                        programItem.ImageSource = WriteFileUtil.GetFolderIcon();
                        break;
                    case "4":
                        /*
                        programItem.WebUrl = programItem.ItemPath;
                        programItem.ImageFlag = Visibility.Hidden;
                        programItem.TextFlag = Visibility.Hidden;
                        programItem.WebFlag = Visibility.Visible;*/
                        programItem.ImageFlag = Visibility.Hidden;
                        programItem.TextFlag = Visibility.Visible;
                        programItem.MuteVisible = Visibility.Hidden;
                        programItem.ResidenceFlag = Visibility.Hidden;
                        programItem.ExitFlag = Visibility.Hidden;
                        programItem.ItemTypeStr = "网页框";
                        break;
                    case "5":
                        programItem.ImageFlag = Visibility.Hidden;
                        programItem.TextFlag = Visibility.Visible;
                        programItem.MuteVisible = Visibility.Hidden;
                        programItem.ResidenceFlag = Visibility.Hidden;
                        programItem.ExitFlag = Visibility.Hidden;
                        programItem.ItemTypeStr = "采集卡";
                        break;
                    case "6":
                        programItem.ImageFlag = Visibility.Hidden;
                        programItem.TextFlag = Visibility.Visible;
                        programItem.MuteVisible = Visibility.Hidden;
                        programItem.ResidenceFlag = Visibility.Hidden;
                        programItem.ExitFlag = Visibility.Visible;
                        programItem.ItemTypeStr = "电脑桌面";
                        break;
                    case "7":
                        programItem.ImageFlag = Visibility.Hidden;
                        programItem.TextFlag = Visibility.Visible;
                        programItem.MuteVisible = Visibility.Hidden;
                        programItem.ResidenceFlag = Visibility.Hidden;
                        programItem.ExitFlag = Visibility.Hidden;
                        programItem.ItemTypeStr = "黑场";
                        break;
                    default:
                        programItem.MuteVisible = Visibility.Hidden;
                        programItem.ResidenceFlag = Visibility.Hidden;
                        programItem.ExitFlag = Visibility.Hidden;
                        //programItem.ItemType = "文件";
                        break;
                }
                //处理屏幕
                ObservableCollection<ScreenItemVo> screenItemVos = new ObservableCollection<ScreenItemVo>();
                if (null != linkItem)
                {
                    screenItemVos = linkItem.ScreenItemVos;
                }
                ObservableCollection<ScreenItemVo> newScreenItemVos = new ObservableCollection<ScreenItemVo>();
                if (null != ScreenVos && ScreenVos.Count > 0)
                {
                    foreach (ScreenVo screen in ScreenVos)
                    {
                        bool isSelected = false;
                        if (null != screenItemVos &&  screenItemVos.Count>0)
                        {
                            foreach (ScreenItemVo screenItemVo1 in screenItemVos)
                            {
                                if (screenItemVo1.Id == screen.Id)
                                {
                                    isSelected = screenItemVo1.IsSelected;
                                    break;
                                }
                            }
                        }
                        ScreenItemVo screenItemVo = new ScreenItemVo(screen.Id, screen.ScreenName, isSelected);
                        newScreenItemVos.Add(screenItemVo);
                    }
                }
                programItem.ScreenItemVos = newScreenItemVos;
                programItem.DisplayModeTabe = DisplayModeTabe;
                programItem.ExitMethodTabe = ExitMethodTabe;
                programItem.DisplayMode = string.IsNullOrEmpty(programItem.DisplayMode) ? "2" : programItem.DisplayMode;
                programItem.ExitMethod = string.IsNullOrEmpty(programItem.ExitMethod) ? "0" : programItem.ExitMethod;
                //处理类型
                switch (programItem.ItemType)
                {
                    case "1":
                        programItem.ImageFlag = Visibility.Visible;
                        programItem.TextFlag = Visibility.Hidden;
                        programItem.ImageSource = WriteFileUtil.GetThumbnailByPath(programItem.ItemPath);
                        programItem.ResidenceFlag = Visibility.Hidden;
                        programItem.ExitFlag = Visibility.Hidden;
                        programItem.MuteVisible = Visibility.Visible;
                        break;
                    case "2":
                        programItem.ImageFlag = Visibility.Visible;
                        programItem.TextFlag = Visibility.Hidden;
                        programItem.MuteVisible = Visibility.Hidden;
                        programItem.ResidenceFlag = Visibility.Hidden;
                        programItem.ExitFlag = Visibility.Visible;
                        programItem.ImageSource = WriteFileUtil.GetThumbnailByPath(programItem.ItemPath); ;
                        break;
                    case "3":
                        programItem.ImageFlag = Visibility.Visible;
                        programItem.TextFlag = Visibility.Hidden;
                        programItem.MuteVisible = Visibility.Hidden;
                        programItem.ResidenceFlag = Visibility.Visible;
                        programItem.ExitFlag = Visibility.Visible;
                        programItem.ImageSource = WriteFileUtil.GetFolderIcon();
                        break;
                    case "4":
                        /*
                        programItem.WebUrl = programItem.ItemPath;
                        programItem.ImageFlag = Visibility.Hidden;
                        programItem.TextFlag = Visibility.Hidden;
                        programItem.WebFlag = Visibility.Visible;*/
                        programItem.ImageFlag = Visibility.Hidden;
                        programItem.TextFlag = Visibility.Visible;
                        programItem.MuteVisible = Visibility.Hidden;
                        programItem.ResidenceFlag = Visibility.Hidden;
                        programItem.ExitFlag = Visibility.Hidden;
                        programItem.ItemTypeStr = "网页框";
                        break;
                    case "5":
                        programItem.ImageFlag = Visibility.Hidden;
                        programItem.TextFlag = Visibility.Visible;
                        programItem.MuteVisible = Visibility.Hidden;
                        programItem.ItemTypeStr = "采集卡";
                        break;
                    case "6":
                        programItem.ImageFlag = Visibility.Hidden;
                        programItem.TextFlag = Visibility.Visible;
                        programItem.MuteVisible = Visibility.Hidden;
                        programItem.ResidenceFlag = Visibility.Hidden;
                        programItem.ExitFlag = Visibility.Visible;
                        programItem.ItemTypeStr = "电脑桌面";
                        break;
                    case "7":
                        programItem.ImageFlag = Visibility.Hidden;
                        programItem.TextFlag = Visibility.Visible;
                        programItem.MuteVisible = Visibility.Hidden;
                        programItem.ResidenceFlag = Visibility.Hidden;
                        programItem.ExitFlag = Visibility.Hidden;
                        programItem.ItemTypeStr = "黑场";
                        break;
                    default:
                        //programItem.ItemType = "文件";
                        programItem.MuteVisible = Visibility.Hidden;
                        programItem.ExitFlag = Visibility.Hidden;
                        break;
                }
                //programItem.ImageSource = GetIcon(programItem.ItemType);
                tempList1.Add(programItem);
            }
            SourceList.Clear();
            foreach (var programItem1 in tempList1) {
                SourceList.Add(programItem1);
            }
        }
       
        private void initScreenList()
        {
            ScreenVos.Clear();
            string filePath = AppDomain.CurrentDomain.BaseDirectory + "/Config/screenlist.info";
            if (File.Exists(filePath))
            {
                string content = WriteFileUtil.readFile(filePath);
                if (!String.IsNullOrEmpty(content))
                {
                    ScreenVos = JsonConvert.DeserializeObject<ObservableCollection<ScreenVo>>(content);
                }
            }
        }

        public DataTable DisplayModeTabe { get; set; }

        public DataTable ExitMethodTabe { get; set; }
        public ProgramVo Program { get => program; set => program = value; }
        public System.Windows.Controls.ListBox ListBox { get => listBox; set => listBox = value; }
        public ObservableCollection<ProgramItem> TempList { get => tempList; set => tempList = value; }
        public bool PlayFlag { get { return playFlag; } set { this.playFlag = value; RaisePropertyChanged(); } }

        private void initData()
        {
            initScreenList();
            //初始化下拉
            DisplayModeTabe = new DataTable();
            DisplayModeTabe.Columns.Add("状态值");
            DisplayModeTabe.Columns.Add("状态显示");
            DisplayModeTabe.Rows.Add("1", "满屏");
            DisplayModeTabe.Rows.Add("2", "缩放");
            DisplayModeTabe.Rows.Add("3", "缩放16:9");
            DisplayModeTabe.Rows.Add("4", "缩放4:3");

            //出场方式
            ExitMethodTabe = new DataTable();
            ExitMethodTabe.Columns.Add("状态值");
            ExitMethodTabe.Columns.Add("状态显示");
            ExitMethodTabe.Rows.Add("0", "无");
            ExitMethodTabe.Rows.Add("1", "渐变");
            ExitMethodTabe.Rows.Add("2", "左移");
            ExitMethodTabe.Rows.Add("3", "右移");
            ExitMethodTabe.Rows.Add("4","上移");
            ExitMethodTabe.Rows.Add("5", "下移");
            ExitMethodTabe.Rows.Add("6", "百叶");
            ExitMethodTabe.Rows.Add("7", "缩放");
            ExitMethodTabe.Rows.Add("8", "随机");
            SourceList.Clear();
            string content = "";//返回的结果字符串
            /*if (File.Exists(sourceFilePath))
            {
                using (StreamReader sr = new StreamReader(sourceFilePath))
                {
                    content = sr.ReadToEnd();//一次性把文本内容读完
                }
            }
            List<ProgramItem> itemList = new List<ProgramItem>();
            if (!String.IsNullOrEmpty(content))
            {
                itemList = JsonConvert.DeserializeObject<List<ProgramItem>>(content);
            }*/
            if (null != tempSourceList && tempSourceList.Count > 0)
            {
                ObservableCollection<ProgramItem> tempList = tempSourceList;
                foreach (ProgramItem programItem in tempList)
                {
                    programItem.DisplayMode = string.IsNullOrEmpty(programItem.DisplayMode) ? "2" : programItem.DisplayMode;
                    programItem.ExitMethod = string.IsNullOrEmpty(programItem.ExitMethod) ? "0" : programItem.ExitMethod;
                    //处理类型
                    switch (programItem.ItemType)
                    {
                        case "1":
                            programItem.ImageFlag = Visibility.Visible;
                            programItem.TextFlag = Visibility.Hidden;
                            programItem.MuteVisible = Visibility.Visible;
                            programItem.ResidenceFlag = Visibility.Hidden;
                            programItem.ExitFlag = Visibility.Hidden;
                            programItem.ImageSource = WriteFileUtil.GetThumbnailByPath(programItem.ItemPath);
                            break;
                        case "2":
                            programItem.ImageFlag = Visibility.Visible;
                            programItem.TextFlag = Visibility.Hidden;
                            programItem.MuteVisible = Visibility.Hidden;
                            programItem.ResidenceFlag = Visibility.Hidden;
                            programItem.ExitFlag = Visibility.Visible;
                            programItem.ImageSource = WriteFileUtil.GetThumbnailByPath(programItem.ItemPath); ;
                            break;
                        case "3":
                            programItem.ImageFlag = Visibility.Visible;
                            programItem.TextFlag = Visibility.Hidden;
                            programItem.MuteVisible = Visibility.Hidden;
                            programItem.ResidenceFlag = Visibility.Visible;
                            programItem.ExitFlag = Visibility.Visible;
                            programItem.ImageSource = WriteFileUtil.GetFolderIcon();
                            break;
                        case "4":
                            /*
                            programItem1.WebUrl = programItem1.ItemPath;
                            programItem1.ImageFlag = Visibility.Hidden;
                            programItem1.TextFlag = Visibility.Hidden;
                            programItem1.WebFlag = Visibility.Visible;*/
                            programItem.ImageFlag = Visibility.Hidden;
                            programItem.TextFlag = Visibility.Visible;
                            programItem.MuteVisible = Visibility.Hidden;
                            programItem.ResidenceFlag = Visibility.Hidden;
                            programItem.ExitFlag = Visibility.Hidden;
                            programItem.ItemTypeStr = "网页框";
                            break;
                        case "5":
                            programItem.ImageFlag = Visibility.Hidden;
                            programItem.TextFlag = Visibility.Visible;
                            programItem.MuteVisible = Visibility.Hidden;
                            programItem.ResidenceFlag = Visibility.Hidden;
                            programItem.ExitFlag = Visibility.Hidden;
                            programItem.ItemTypeStr = "采集卡";
                            break;
                        case "6":
                            programItem.ImageFlag = Visibility.Hidden;
                            programItem.TextFlag = Visibility.Visible;
                            programItem.ResidenceFlag = Visibility.Hidden;
                            programItem.MuteVisible = Visibility.Hidden;
                            programItem.ExitFlag = Visibility.Visible;
                            programItem.ItemTypeStr = "电脑桌面";
                            break;
                        case "7":
                            programItem.ImageFlag = Visibility.Hidden;
                            programItem.TextFlag = Visibility.Visible;
                            programItem.ItemTypeStr = "黑场";
                            programItem.MuteVisible = Visibility.Hidden;
                            programItem.ResidenceFlag = Visibility.Hidden;
                            programItem.ExitFlag = Visibility.Hidden;
                            break;
                        default:
                            //programItem.ItemType = "文件";
                            programItem.MuteVisible = Visibility.Hidden;
                            programItem.ResidenceFlag = Visibility.Hidden;
                            programItem.ExitFlag = Visibility.Hidden;
                            break;
                    }
                    //处理屏幕
                    ObservableCollection<ScreenItemVo> newScreenItemVos = new ObservableCollection<ScreenItemVo>();
                    if (null != ScreenVos && ScreenVos.Count > 0)
                    {
                        foreach (ScreenVo screen in ScreenVos)
                        {
                            bool isSelected = false;
                            ScreenItemVo screenItemVo = new ScreenItemVo(screen.Id, screen.ScreenName, isSelected);
                            newScreenItemVos.Add(screenItemVo);
                        }
                    }
                    programItem.ScreenItemVos = newScreenItemVos;
                    programItem.DisplayModeTabe = DisplayModeTabe;
                    programItem.ExitMethodTabe = ExitMethodTabe;
                   // programItem.ImageSource = GetIcon(programItem.ItemType);
                    SourceList.Add(programItem);
                }
            }
        }

        private ProgramItem handleItem(ProgramItem programItem1) {
            programItem1.DisplayModeTabe = DisplayModeTabe;
            programItem1.ExitMethodTabe = ExitMethodTabe;
            //处理屏幕
            ObservableCollection<ScreenItemVo> newScreenItemVos = new ObservableCollection<ScreenItemVo>();
            foreach (ScreenVo screen in ScreenVos)
            {
                bool isSelected = false;
                ScreenItemVo screenItemVo = new ScreenItemVo(screen.Id, screen.ScreenName, isSelected);
                newScreenItemVos.Add(screenItemVo);
            }
            programItem1.ScreenItemVos = newScreenItemVos;
            //programItem1.ImageSource = GetIcon(programItem1.ItemType);
            //处理类型
            programItem1.DisplayMode = string.IsNullOrEmpty(programItem1.DisplayMode) ? "2" : programItem1.DisplayMode;
            programItem1.ExitMethod = string.IsNullOrEmpty(programItem1.ExitMethod) ? "0" : programItem1.ExitMethod;
            switch (programItem1.ItemType)
            {
                case "1":
                    programItem1.ImageFlag = Visibility.Visible;
                    programItem1.TextFlag = Visibility.Hidden;
                    programItem1.MuteVisible = Visibility.Visible;
                    programItem1.ResidenceFlag = Visibility.Hidden;
                    programItem1.ExitFlag = Visibility.Hidden;
                    programItem1.ImageSource = WriteFileUtil.GetThumbnailByPath(programItem1.ItemPath);
                    break;
                case "2":
                    programItem1.ImageFlag = Visibility.Visible;
                    programItem1.TextFlag = Visibility.Hidden;
                    programItem1.MuteVisible = Visibility.Hidden;
                    programItem1.ResidenceFlag = Visibility.Hidden;
                    programItem1.ExitFlag = Visibility.Visible;
                    programItem1.ImageSource = WriteFileUtil.GetThumbnailByPath(programItem1.ItemPath); ;
                    break;
                case "3":
                    programItem1.ImageFlag = Visibility.Visible;
                    programItem1.TextFlag = Visibility.Hidden;
                    programItem1.MuteVisible = Visibility.Hidden;
                    programItem1.ResidenceFlag = Visibility.Visible;
                    programItem1.ExitFlag = Visibility.Visible;
                    programItem1.ImageSource = WriteFileUtil.GetFolderIcon();
                    break;
                case "4":
                    /*
                    programItem1.WebUrl = programItem1.ItemPath;
                    programItem1.ImageFlag = Visibility.Hidden;
                    programItem1.TextFlag = Visibility.Hidden;
                    programItem1.WebFlag = Visibility.Visible;*/
                    programItem1.ImageFlag = Visibility.Hidden;
                    programItem1.TextFlag = Visibility.Visible;
                    programItem1.MuteVisible = Visibility.Hidden;
                    programItem1.ResidenceFlag = Visibility.Hidden;
                    programItem1.ExitFlag = Visibility.Hidden;
                    programItem1.ItemTypeStr = "网页框";
                    break;
                case "5":
                    programItem1.ImageFlag = Visibility.Hidden;
                    programItem1.TextFlag = Visibility.Visible;
                    programItem1.MuteVisible = Visibility.Hidden;
                    programItem1.ResidenceFlag = Visibility.Hidden;
                    programItem1.ExitFlag = Visibility.Hidden;
                    programItem1.ItemTypeStr = "采集卡";
                    break;
                case "6":
                    programItem1.ImageFlag = Visibility.Hidden;
                    programItem1.TextFlag = Visibility.Visible;
                    programItem1.ResidenceFlag = Visibility.Hidden;
                    programItem1.MuteVisible = Visibility.Hidden;
                    programItem1.ExitFlag = Visibility.Visible;
                    programItem1.ItemTypeStr = "电脑桌面";
                    break;
                case "7":
                    programItem1.ImageFlag = Visibility.Hidden;
                    programItem1.TextFlag = Visibility.Visible;
                    programItem1.ItemTypeStr = "黑场";
                    programItem1.MuteVisible = Visibility.Hidden;
                    programItem1.ResidenceFlag = Visibility.Hidden;
                    programItem1.ExitFlag = Visibility.Hidden;
                    break;
                default:
                    //programItem.ItemType = "文件";
                    programItem1.MuteVisible = Visibility.Hidden;
                    programItem1.ResidenceFlag = Visibility.Hidden;
                    programItem1.ExitFlag = Visibility.Hidden;
                    break;
            }
            return programItem1;
        }
    }
}
